package com.example.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.entity.Roles;
import com.example.entity.RolesVO;
import com.example.entity.TreeMenu;
import com.example.entity.UserRoles;
import com.example.service.RolesService;
import com.example.service.UserRolesService;

@RestController
public class RolesController {
	
	
	
	@Autowired
	private UserRolesService userRolesService;
	
	@Autowired
	private RolesService roleService;
	
	
	
	/**
	 *添加
	 * @param id
	 * @return
	 */
	@PostMapping(path = "/addUserRoles")
	public int upUserRoles(UserRoles userRoles) { // 受影响的行数 
		return userRolesService.addUserRoles(userRoles);
	}
	/**
	 *添加
	 * @param id
	 * @return
	 */
	@PostMapping(path = "/delUserRoles")
	public int delUserRoles(UserRoles userRoles) { // 受影响的行数 
		return userRolesService.delUserRoles(userRoles);
		

	}
	
	/**
	 * 角色按条件查询
	 * @param rolesVO
	 * @return
	 */
	@PostMapping("/findAllRoles")
	public Map<String, Object> findAllRoles(RolesVO rolesVO){
		Map<String, Object> reMap=new HashMap<String, Object>();
		rolesVO.setOffset((rolesVO.getPage()-1)*rolesVO.getLimit());
		List<Roles> list=roleService.findAllRoles(rolesVO);
		int count=roleService.getCount(rolesVO);
		reMap.put("code","0");
		reMap.put("msg", "success");
		reMap.put("count", count);
		reMap.put("data", list);
		return reMap;
	}
	

	
    /**
     * 添加角色
     * @param roles
     * @param name
     * @return
     */
	@PostMapping("/addRoles")
	public Map<String, Object> addRoles(Roles roles,String name){
		Map<String, Object> relMap=new HashMap<String, Object>();
		int rows=roleService.addroles(roles);
		relMap.put("code",rows>0? "0":"1");
		relMap.put("msg",rows>0? "增加成功":"增加失败");
		return relMap;
	}
	
	
	/**
	 * 修改角色
	 * @param roles
	 * @return
	 */
	@PostMapping("/upRoles")
	public Map<String,Object> upRoles(Roles roles){
		Map<String,Object> relMap=new HashMap<String,Object>();
		int rows=roleService.upRoles(roles);
		relMap.put("code", rows>0?"0":"1");
		relMap.put("msg", rows>0?"修改成功":"修改失败");
	    return relMap;
	}
	
    /**
     * 删除角色
     * @param Id
     * @return
     */
	@PostMapping("/delRoles")
	public Map<String, Object> delBook(int id){
		Map<String, Object> relMap=new HashMap<String, Object>();
		int rows=roleService.delRoles(id);
		relMap.put("code",rows>0? "0":"1");
		relMap.put("msg",rows>0? "删除成功":"删除失败");
		return relMap;
	}
	
	
	/**
	 * 角色权限全查
	 * @return
	 */
//	@PostMapping(path = "/selectModulesAll")
//	public Map<String, Object> selectModulesAll() { // 受影响的行数
//		
//		List<Modules> list = roleService.selectmodulesAll();
//		Map<String, Object> rsMap =new HashMap<String, Object>();
//		rsMap.put("code", "code");
//		rsMap.put("msg","");
//		rsMap.put("count", list.size());
//		rsMap.put("data",list);
//		return rsMap;
//
//	}
	
	/**
	 *根据modules获取权限
	 * @param id
	 * @return
	 */
//	@PostMapping(path = "/getModulesByUid")
//	public List<TreeMenu> getModulesByUid(String roleids) { // 受影响的行数
//		List<TreeMenu> list = treeMenuService.getTreeMenu2(roleids);
////		for (TreeMenu treeMenu : list) {
////			if (treeMenu.getParentid()==0) {
////				treeMenu.setChecked(false);
////			}
////		}
//		return list;
//	}
	 
	
	
	/**
	* 查询所有的权限
	* 这里是查询所有的权限模块，然后给夫模块赋予false前台才可以显示树形组件
	* @return List
	*/
	public List<TreeMenu> getRolesTreeNodes(Integer rolesIds) {
		List<TreeMenu> modulesTreeNodes = roleService.selectAllTree(rolesIds);
//		for (TreeMenu modulesTreeNode : modulesTreeNodes) {
//			if (modulesTreeNode.getParentid()==0) {
//				modulesTreeNode.setChecked(false);
//			}
//		}
		return modulesTreeNodes;
	}

	/**
	* 生成模块类型树
	* @return List
	*/
	@RequestMapping("/GetModulesByRoleIdLayUI")
	public List<TreeMenu> getRolesTreeNode(Integer rolesIds) {
		List<TreeMenu> rolesTreeList = this.getRolesTreeNodes(rolesIds);
		List<TreeMenu> rolesTreeNodes = new ArrayList<TreeMenu>();
		System.out.println("----------------");
		for (TreeMenu rolesTreeNode : rolesTreeList) {
			System.out.println(rolesTreeNode.getParentid());
			if (rolesTreeNode.getParentid()==0) {
				rolesTreeNodes.add(findChildren(rolesTreeNode, rolesTreeList));
			}
		}
		return rolesTreeNodes;
	}

	/**
	* 查找子类型
	* @return List
	*/
	public TreeMenu findChildren(TreeMenu rolesTreeNode, List<TreeMenu> rolesTreeList) {
		for (TreeMenu item : rolesTreeList) {
			 if (rolesTreeNode.getId()==item.getParentid()) {
				 if(rolesTreeNode.getChildren()==null) {
					 rolesTreeNode.setChildren(new ArrayList<TreeMenu>());
				 } 
				 rolesTreeNode.getChildren().add(findChildren(item,rolesTreeList));
			 } 
		}
		return rolesTreeNode;
	}

	/**
	* 修改角色权限
	* 首先先要根据角色id把本身的权限删除，然后在进行添加权限
	* @return boolean 
	*/
	@PostMapping("/SetSysRights")
	public Map<String, Object> SetSysRights(String id,String parentIds,String rId){
		
		roleService.deleteByUidAndModuleid(rId, "");
		
		String[] m= parentIds.split(",");
		for (int i = 0; i < m.length; i++) {
		roleService.insertByUidAndModuleid(id,rId, m[i]);
		}
		Map<String, Object>  map = new HashMap<>();
		map.put("success", true);
		return map;
	}
	
	
}
